<!-- htmlhint doctype-first:false -->
<table id="prompts-table" class="min-w-full divide-y divide-gray-200">
  <thead class="bg-gray-50 dark:bg-gray-700">
    <tr>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-12">S. No.</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">Name</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">Description</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-20">Tags</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-20">Owner</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-20">Team</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-16">Visibility</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider w-12">Status</th>
      <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">Actions</th>
    </tr>
  </thead>
  <tbody id="prompts-table-body" class="bg-white divide-y divide-gray-200 dark:bg-gray-900 dark:divide-gray-700">
{% for prompt in data %}
<tr>
    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 w-12 dark:text-gray-100">{{ (pagination.page - 1) * pagination.per_page + loop.index }}</td>
    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 dark:text-gray-100">{{ prompt.name }}</td>
    <td class="px-6 py-4 whitespace-normal break-words text-sm text-gray-500 dark:text-gray-300">{% set clean_desc = (prompt.description or "") | replace('\n', ' ') | replace('\r', ' ') %} {% set refactor_desc = clean_desc | striptags | trim | escape %} {% if refactor_desc | length is greaterthan 220 %} {{ refactor_desc[:400] + "..." }} {% else %} {{ refactor_desc }} {% endif %}</td>
    <td class="px-6 py-4 whitespace-nowrap">{% if prompt.tags %} {% for tag in prompt.tags %}<span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-800 mr-1 mb-1">{{ tag.id }}</span>{% endfor %} {% else %}<span class="text-gray-500 dark:text-gray-300 text-xs">None</span>{% endif %}</td>
  <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-300 w-20">{{ prompt.owner_email }}</td>
  <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-300 w-20">{% if prompt.team %}{{ prompt.team }}{% else %}<span class="text-gray-400 dark:text-gray-600 text-xs">None</span>{% endif %}</td>
    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-300 w-16">{% if prompt.visibility == 'public' %}<span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-green-100 text-green-800">🌍 Public</span>{% elif prompt.visibility == 'team' %}<span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-800">👥 Team</span>{% else %}<span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-gray-100 text-gray-800">🔒 Private</span>{% endif %}</td>
  <td class="px-6 py-4 whitespace-nowrap text-sm w-12"><div class="relative group">{% set enabled = prompt.enabled %}<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full {% if enabled %}bg-green-100 text-green-800{% else %}bg-red-100 text-red-800{% endif %}">{% if enabled %}Active{% else %}Inactive{% endif %}</span></div></td>
    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
      <div class="grid grid-cols-2 gap-x-2 gap-y-0 max-w-48">
        <!-- Row 1: Test -->
        <button
          onclick="testPrompt('{{ prompt.id }}')"
          class="col-span-2 flex items-center justify-center px-2 py-1 text-xs font-medium rounded-md text-purple-600 hover:text-purple-900 hover:bg-purple-50 dark:text-purple-400 dark:hover:bg-purple-900/20 transition-colors"
          x-tooltip="'💡Test this Prompt with sample arguments'"
        >
          Test
        </button>

        <!-- Row 2: View | Edit -->
        <button onclick="viewPrompt('{{ prompt.id }}')" class="flex items-center justify-center px-2 py-1 text-xs font-medium rounded-md text-indigo-600 hover:text-indigo-900 hover:bg-indigo-50 dark:text-indigo-400 dark:hover:bg-indigo-900/20 transition-colors">View</button>
        <button onclick="editPrompt('{{ prompt.id }}')" class="flex items-center justify-center px-2 py-1 text-xs font-medium rounded-md text-green-600 hover:text-green-900 hover:bg-green-50 dark:text-green-400 dark:hover:bg-green-900/20 transition-colors">Edit</button>

        <!-- Row 3 & 4: Activate/Deactivate | Delete -->
        <div class="col-span-2 flex flex-col space-y-1">
          <form method="POST" action="{{ root_path }}/admin/prompts/{{ prompt.id }}/toggle" class="contents" onsubmit="return handleToggleSubmit(event, 'prompts')">
            <input type="hidden" name="activate" value="{{ 'false' if prompt.enabled else 'true' }}" />
            <button type="submit" class="flex items-center justify-center px-2 py-1 text-xs font-medium rounded-md {% if prompt.enabled %}text-yellow-600 hover:text-yellow-900 hover:bg-yellow-50 dark:text-yellow-400 dark:hover:bg-yellow-900/20{% else %}text-green-600 hover:text-green-900 hover:bg-green-50 dark:text-green-400 dark:hover:bg-green-900/20{% endif %}">{% if prompt.enabled %}Deactivate{% else %}Activate{% endif %}</button>
          </form>
          <form method="POST" action="{{ root_path }}/admin/prompts/{{ prompt.id }}/delete" class="contents" onsubmit="return handleDeleteSubmit(event, 'prompt', '{{ prompt.name }}')">
            <button type="submit" class="flex items-center justify-center px-2 py-1 text-xs font-medium rounded-md text-red-600 hover:text-red-900 hover:bg-red-50 dark:text-red-400 dark:hover:bg-red-900/20 transition-colors">Delete</button>
          </form>
        </div>
      </div>
    </td>
  </tr>
{% endfor %}
  </tbody>
</table>

<!-- Out-of-band swap for pagination controls -->
<div id="prompts-pagination-controls" hx-swap-oob="true">
  {% set base_url = root_path + '/admin/prompts/partial' %}
  {% set hx_target = '#prompts-table' %}
  {% set hx_indicator = '#prompts-loading' %}
  {% set query_params = {'include_inactive': include_inactive} %}
  {% include 'pagination_controls.html' %}
</div>
